package jp.ne.iforce.android.jsendtest;

import jp.ne.iforce.android.jsendtest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class JsendActivity extends Activity  implements Runnable  {

	private EditText editText;
	private ProgressDialog progressDialog;
	private Menu menu;
	private static final int MENU1 = 1;
	public C2dmSend c2dm = new C2dmSend();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.main);

        // 送信ボタン
        ((Button) findViewById(R.id.Hello_button)).setOnClickListener(Hello_buttonListener);
        ((Button) findViewById(R.id.CallPlease_button)).setOnClickListener(CallPlease_buttonListener);
        ((Button) findViewById(R.id.Goinghome_button)).setOnClickListener(Goinghome_buttonListener);
        ((Button) findViewById(R.id.Hurryup_button)).setOnClickListener(Hurryup_buttonListener);
        ((Button) findViewById(R.id.Goodbye_button)).setOnClickListener(Goodbye_buttonListener);
        ((Button) findViewById(R.id.Send_button)).setOnClickListener(Send_buttonListener);
    }

    public void startProgress(){
    	progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("送信中・・・");
		progressDialog.setIndeterminate(false);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.show();

    }
    public void run() {
    	c2dm.SendMessage("message");
		handler.sendEmptyMessage(0);
	}


    private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
		}
	};

	private void sendMessage(String str){
		c2dm.message = str;
		startProgress();
		Thread thread = new Thread(JsendActivity.this);
		thread.start();
	}

    OnClickListener Hello_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage(getString(R.string.HELLO));
    	}

    };

    OnClickListener CallPlease_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage(getString(R.string.CallPlease));
    	}
    };

    OnClickListener Goinghome_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage(getString(R.string.Goinghome));
    	}
    };

    OnClickListener Hurryup_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage(getString(R.string.Hurryup));
    	}
    };

    OnClickListener Goodbye_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage(getString(R.string.Goodbye));
    	}
    };

    OnClickListener Send_buttonListener = new View.OnClickListener() {
    	public void onClick(View v) {
    		editText = (EditText) findViewById(R.id.Text_edit);
    		String editStr = editText.getText().toString();
    		if(editStr == null || editStr.equals("") ){
        		Toast toast = Toast.makeText(JsendActivity.this, getString(R.string.PleaseInputMessage), Toast.LENGTH_SHORT);
        		toast.show();
    		}else{
    			sendMessage(editStr.trim());
    		}
    		editText.setText("");

    	}
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu1) {
    	menu = menu1;
        menu.add(Menu.NONE, MENU1, Menu.NONE, "地震警報");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean ret = true;
        switch (item.getItemId()) {
        default:
            ret = super.onOptionsItemSelected(item);
            break;
        case MENU1:
        	Intent i = new Intent(getApplicationContext(),EarthquakeActivity.class);
        	startActivityForResult(i,0);
        	finish();
        	break;
        }
        return ret;
    }

}